/**
 ******************************************************************************
 * @file      dddd.c
 * @brief     C Source file of dddd.c.
 * @details   This file including all API functions's implement of dddd.c.
 * @copyright  Copyright(C)
 ******************************************************************************
 */

/*------------------------------------------------------------------------------
 Section: Includes
 ------------------------------------------------------------------------------*/
#include <stdio.h>

#include "key.h"
#include <drivers/pin.h>
/*------------------------------------------------------------------------------
 Section: Macro Definitions
 ------------------------------------------------------------------------------*/


/*------------------------------------------------------------------------------
 Section: Type Definitions
 ------------------------------------------------------------------------------*/


/*------------------------------------------------------------------------------
 Section: Global Function prototypes
 ------------------------------------------------------------------------------*/
/* None */

/*------------------------------------------------------------------------------
 Section: Local Function prototypes
 ------------------------------------------------------------------------------*/
void Key_Thread(void *arg);

/*------------------------------------------------------------------------------
 Section: Global Variables
 ------------------------------------------------------------------------------*/
/* None */

/*------------------------------------------------------------------------------
 Section: Local Variables
 ------------------------------------------------------------------------------*/
KEY_FIFO_T s_tKey;

/*------------------------------------------------------------------------------
 Section: Function Definitions
 ------------------------------------------------------------------------------*/
/**
 ******************************************************************************
 * @brief       Key_Threads
 * @param
 * @return
 * @details
 ******************************************************************************
 */
void Key_Thread(void *arg)
{
    uint8_t ucKeyCode = KEY_NONE;      /* 按键代码 */

    while(1)
    {
        switch(ucKeyCode)
        {
            case KEY_1_DOWN:           /* K1键按下 */
                rt_kprintf("K1键按下\r\n");
                break;

            case KEY_1_UP:             /* K1键弹起 */
                rt_kprintf("K1键弹起\r\n");
                break;

            case KEY_2_DOWN:           /* K2键按下 */
                rt_kprintf("K2键按下\r\n");
                break;

            case KEY_2_UP:             /* K2键弹起 */
                rt_kprintf("K2键弹起\r\n");
                break;

            case KEY_3_DOWN:           /* K3键按下 */
                rt_kprintf("K3键按下\r\n");
                break;

            case KEY_3_UP:             /* K3键弹起 */
                rt_kprintf("K3键弹起\r\n");
                break;

            default:
                /* 其它的键值不处理 */
                break;
        }
    }
}

/**
 ******************************************************************************
 * @brief       InitKey
 * @param
 * @return
 * @details
 ******************************************************************************
 */
void InitKey(void)
{
    rt_pin_mode(KEY1, PIN_MODE_INPUT);
    rt_pin_mode(KEY2, PIN_MODE_INPUT);
    rt_pin_mode(KEY3, PIN_MODE_INPUT);
}


/**
 ******************************************************************************
 * @brief       GetKey
 * @param
 * @return
 * @details
 ******************************************************************************
 */
uint8_t GetKey(void)
{
    uint8_t ret;

    if (s_tKey.Read == s_tKey.Write)
    {
        return KEY_NONE;
    }
    else
    {
        ret = s_tKey.Buf[s_tKey.Read];

        if (++s_tKey.Read >= KEY_FIFO_SIZE)
        {
            s_tKey.Read = 0;
        }
        return ret;
    }
}

/**
 ******************************************************************************
 * @brief       bsp_InitKey
 * @param
 * @return
 * @details
 ******************************************************************************
 */


/**
 ******************************************************************************
 * @brief       bsp_InitKey
 * @param
 * @return
 * @details
 ******************************************************************************
 */

/*------------------------------End of dddd.c----------------------------*/
